// Cpw_01 [Cpw-OpenGL Triangle].nova // Using namespace declarations. using Library.CPW; using Library.OpenGL; // The application class. class CpwTriangleApp { // Static data members. private static CPW cpw; // Application class's "main" function. public static void main( String[] args ) { // Create a new CPW context object. cpw = new CPW( ); // Initialize the CPW context object. cpw.cpwInitContext( ); // Create a window. cpw.cpwCreateWindowEx( "Cpw_01 [Cpw-OpenGL Triangle]", 100, 100, 640, 480 ); // Register the event callbacks. cpw.cpwCreateCallback( createCallback ); cpw.cpwDisplayCallback( displayCallback ); cpw.cpwReshapeCallback( reshapeCallback ); // Enter the main rendering loop. cpw.cpwMainLoop( ); // Shutdown the CPW context. cpw.cpwFreeContext( ); } // Window create / close event callback. private static void createCallback( CPW cpw, uint winID, bool flag ) { Stream.writeLine( "createCallback - called" ); if ( flag == false ) { // Window close event. cpw.cpwDestroyWindow( winID ); } } // Window draw event callback. private static void displayCallback( CPW cpw, uint winID ) { Stream.writeLine( "displayCallback - called" ); OpenGL.glClear( OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT ); // Draw a triangle. OpenGL.glBegin( OpenGL.GL_TRIANGLES ); OpenGL.glColor3f( 0.0f, 0.0f, 1.0f ); // Blue. OpenGL.glVertex3f( 0.0f, 1.0f, 0.0f ); // Top OpenGL.glColor3f( 1.0f, 0.0f, 0.0f ); // Red. OpenGL.glVertex3f( -1.0f, -1.0f, 0.0f ); // Bottom left. OpenGL.glColor3f( 0.0f, 1.0f, 0.0f ); // Green. OpenGL.glVertex3f( 1.0f, -1.0f, 0.0f ); // Bottom right. OpenGL.glEnd( ); cpw.cpwSwapWindowBuffers( winID ); } // Window resize event callback. private static void reshapeCallback( CPW cpw, uint winID, uint w, uint h ) { Stream.writeLine( "reshapeCallback - called" ); // Prevent a divide by zero. if ( h == 0 ) { h = 1; } // Set the viewport to be the entire window. OpenGL.glViewport( 0, 0, (int)w, (int)h ); // Set the perspective. OpenGL.glMatrixMode( OpenGL.GL_PROJECTION ); OpenGL.glLoadIdentity( ); OpenGL.gluPerspective( 45.0, (double)w / (double)h, 1.0, 150.0 ); OpenGL.glMatrixMode( OpenGL.GL_MODELVIEW ); // Set the view. OpenGL.glLoadIdentity( ); OpenGL.gluLookAt( 0.0, 0.0, 4.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0); } }